<?php
namespace App\Controller;
use Think\Controller;
import("Org.Util.Jpush");
class BaseController extends Controller {

    //验证安全性
    public function Security($checkToken = true){

        $json_arr = $_POST;

        //系统维护中...
        // $this->backToClient(2,'系统维护中...');

        //验证token
        // if($checkToken){
        //     $token['user_token'] = $json_arr['user_token'];
        //     if(empty(M('user')->where($token)->find())){
        //         $this->backToClient(3,'您的账号已经在别处登录，请重新登录');
        //     }
        // }

        // if (!checkSign($json_arr)) {
        //     $this->backToClient(5, '验证签名未通过!');
        // }


        return $json_arr;
    }

    //验证签名
    public function checkSign($params = array()) {
        ksort($params);
        $paramsStr = '';

        foreach ($params as $key=>$value) {
            if ($key != 'sign') {
                $paramsStr .= $key.'='.$value.'&';
            }
        }

        if ($paramsStr) {
            $paramsStr = substr($paramsStr, 0, -1);
            $paramsStr = $paramsStr.C('AES_KEY');
        }

        $sign = strtolower(md5($paramsStr));
        return $sign == strtolower($params['sign']) ? true : false;
    }

    //返回客户端数据
	public function backToClient($status,$msg,$data = array()){
        $result['status'] = $status;
        $result['msg'] = $msg;
        $result['data'] = $data;
        $this->ajaxReturn($result);
        exit();
    }

    //创建32位随机码
    public function createRandom() {
        static $guid = '';
        $uid = uniqid("", true);
        $data .= $_SERVER['REQUEST_TIME'];
        $data .= $_SERVER['HTTP_USER_AGENT'];
        $data .= $_SERVER['LOCAL_ADDR'];
        $data .= $_SERVER['LOCAL_PORT'];
        $data .= $_SERVER['REMOTE_ADDR'];
        $data .= $_SERVER['REMOTE_PORT'];
        $hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data)));
        $guid = substr($hash,  0,  8) .
                substr($hash,  8,  4) .
                substr($hash, 12,  4) .
                substr($hash, 16,  4) .
                substr($hash, 20, 12);
        return $guid;
    }

    //推送
    public function push($receiver_value,$content,$jpush_type){
        $jpush_type = $jpush_type;
        $pushObj = new \Jpush($jpush_type['app_key'],$jpush_type['master_secret'],$jpush_type['url']);
        $receiver_value = $receiver_value;
        $content = $content;
        $m_type = 'http';
        $m_txt = 'clinic';
        $m_time = '600';
        $result = $pushObj->push($receiver_value,$content,$m_type,$m_txt,$m_time);
        $result_arr = json_decode($result,TRUE);
        return $result_arr;
    }

    /**
    * 发送短信验证码
    * @param user_phone      要发送的手机号码
    */
    public function sendMsgCode(){

        // $_POST['user_phone'] = '13811341066';
        $this->backToClient(5,'版本升级，请下载最新版APP');

        $phonenums = $_POST['user_phone'];
        if (!isMobile($phonenums)) {
            $this->backToClient(2,'手机号码格式错误');
        }

        $black_ip_arr = ['58.58.176.13'];

        $condition['msgtemp_ip'] = get_client_ip1();
        //可疑ip
        if (in_array($condition['msgtemp_ip'], $black_ip_arr)) {
            $this->backToClient(5,'黑名单ip');
        }
        $condition['msgtemp_phone'] = $_POST['user_phone'];
        $condition['msgtemp_date'] = date('Y-m-d',time());
        if($temp_arr = M('msgtemp')->where($condition)->find()){
            //次数+1
            $temp_arr['msgtemp_count'] += 1;
            M('msgtemp')->save($temp_arr);
            if ($temp_arr['msgtemp_count'] > 3) {
                $this->backToClient(3,'超出每天发送短信次数');
            }
        }else{
            M('msgtemp')->add($condition);
        }

        // $condition2['user_phone'] = $phonenums;
        // if (M('user')->where($condition2)->find()) {
        //     $this->backToClient(3,'手机号码已经被注册');
        // }
        $code = createRandomCode(4);
        alidayucode($code, $phonenums); //发送短信
        $this->backToClient(1,'获取短信验证码成功',$code);
    }

    /**
    * 发送短信验证码
    * @param user_phone      要发送的手机号码
    * @param sign            验签
    * @param time            时间戳
    */
    public function sendMsgCodeApp(){

        // $this->backToClient(5,'短信功能维护中。。。');

        if (intval($_POST['time'] < time() - 30)) {
            $this->backToClient(11,'请求已过期');
        }

        $sign = md5($_POST['time'].md5($_POST['user_phone']).'midilevel');
        if ($sign != $_POST['sign']) {
            $this->backToClient(10,'验签错误');
        }

        $phonenums = $_POST['user_phone'];
        // if (!isMobile($phonenums)) {
        //     $this->backToClient(2,'手机号码格式错误');
        // }

        // $black_ip_arr = ['58.58.176.13'];

        // $condition['msgtemp_ip'] = get_client_ip1();
        // //可疑ip
        // if (in_array($condition['msgtemp_ip'], $black_ip_arr)) {
        //     $this->backToClient(5,'黑名单ip');
        // }
        // $condition['msgtemp_phone'] = $_POST['user_phone'];
        // $condition['msgtemp_date'] = date('Y-m-d',time());
        // if($temp_arr = M('msgtemp')->where($condition)->find()){
        //     //次数+1
        //     $temp_arr['msgtemp_count'] += 1;
        //     M('msgtemp')->save($temp_arr);
        //     if ($temp_arr['msgtemp_count'] > 3) {
        //         $this->backToClient(3,'超出每天发送短信次数');
        //     }
        // }else{
        //     M('msgtemp')->add($condition);
        // }

        // $condition2['user_phone'] = $phonenums;
        // if (M('user')->where($condition2)->find()) {
        //     $this->backToClient(3,'手机号码已经被注册');
        // }
        $code = createRandomCode(4);
        alidayucode($code, $phonenums); //发送短信
        $this->backToClient(1,'获取短信验证码成功',$code);
    }

    /**
    * 发送短信验证码
    * @param user_phone      要发送的手机号码
    */
    public function sendMsgCodeWeb(){

        // $_POST['user_phone'] = '13811341066';

        if (!check_verify($_POST['verify'])) {
            $this->backToClient(6,'图形验证码输入错误');
        }

        $phonenums = $_POST['user_phone'];
        // if (!isMobile($phonenums)) {
        //     $this->backToClient(2,'手机号码格式错误');
        // }

        // $black_ip_arr = ['58.58.176.13'];

        // $condition['msgtemp_ip'] = get_client_ip1();
        // //可疑ip
        // if (in_array($condition['msgtemp_ip'], $black_ip_arr)) {
        //     $this->backToClient(5,'黑名单ip');
        // }
        // $condition['msgtemp_phone'] = $_POST['user_phone'];
        // $condition['msgtemp_date'] = date('Y-m-d',time());
        // if($temp_arr = M('msgtemp')->where($condition)->find()){
        //     //次数+1
        //     $temp_arr['msgtemp_count'] += 1;
        //     M('msgtemp')->save($temp_arr);
        //     if ($temp_arr['msgtemp_count'] > 3) {
        //         $this->backToClient(4,'超出每天发送短信次数');
        //     }
        // }else{
        //     M('msgtemp')->add($condition);
        // }

        // $condition2['user_phone'] = $phonenums;
        // if (M('user')->where($condition2)->find()) {
        //     $this->backToClient(3,'手机号码已经被注册');
        // }
        $code = createRandomCode(4);
        alidayucode($code, $phonenums); //发送短信
        $this->backToClient(1,'获取短信验证码成功',$code);
    }

    public function getUserSig($id){

        if (empty($id)) {
            $this->backToClient(11,'获取UserSig失败');
        }

        Vendor('TLS.Tls');
        $pri_key = "MHQCAQEEICU8wjHEEx2V0nVWSN1PlZ6u6IjQaUDq6zxES4o5DesVoAcGBSuBBAAKoUQDQgAE9iEVDp2mqyvxYvakuYcc02rziQanOd1GtJLDWfuXeMELY750Mqr/QjrwGH+ly6XW+cUCKlRhRys8SFi3SpIlKw==";
        //$pri_key = 'MIGEAgEAMBAGByqGSM49AgEGBSuBBAAKBG0wawIBAQQgJTzCMcQTHZXSdVZI3U+Vnq7oiNBpQOrrPERLijkN6xWhRANCAAT2IRUOnaarK/Fi9qS5hxzTavOJBqc53Ua0ksNZ+5d4wQtjvnQyqv9COvAYf6XLpdb5xQIqVGFHKzxIWLdKkiUr';
        $api = new \TLSSigAPI();

        // 设置在腾讯云申请的appid
        $api->SetAppid(1400031857);

        // 生成usersig需要先设置私钥
        $api->SetPrivateKey($pri_key);

        // 生成usersig
        $sig = $api->genSig($id);

        return $sig;
    }

}